package com.carkeeper.department.service.impl;


import com.carkeeper.department.dao.DepartmentMapper;
import com.carkeeper.pojo.Department;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class DepartmentServiceImpl implements com.carkeeper.department.service.DepartmentService {

    //注入DepartmentMapper
    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<Department> findAll() {

        List<Department> list = departmentMapper.selectAll();

        return list;
    }

    /**
     * 添加部门
     * @param department
     */
    @Override
    public void add(Department department) {

         departmentMapper.insert(department);
    }

    /**
     * 分页查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Department> findPage(Map searchMap, int page, int size) {

        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        return (Page<Department>)departmentMapper.selectByExample(example);
    }

    /**
     * 修改部门
     * @param department
     */
    @Override
    public void update(Department department) {

        departmentMapper.updateByPrimaryKey(department);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id) {

        departmentMapper.deleteByPrimaryKey(id);
    }

    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Department.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // 分类名称
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                criteria.andLike("name", "%" + searchMap.get("name") + "%");
            }
            // 是否显示
            if (searchMap.get("isShow") != null && !"".equals(searchMap.get("isShow"))) {
                criteria.andEqualTo("isShow", searchMap.get("isShow"));
            }
            // 是否导航
            if (searchMap.get("isMenu") != null && !"".equals(searchMap.get("isMenu"))) {
                criteria.andLike("isMenu", "%" + searchMap.get("isMenu") + "%");
            }

            // 分类ID
            if (searchMap.get("id") != null) {
                criteria.andEqualTo("id", searchMap.get("id"));
            }

            // 排序
            if (searchMap.get("seq") != null) {
                criteria.andEqualTo("seq", searchMap.get("seq"));
            }
            // 上级ID
            if (searchMap.get("parentId") != null) {
                criteria.andEqualTo("parentId", searchMap.get("parentId"));
            }
            // 模板ID
            if (searchMap.get("templateId") != null) {
                criteria.andEqualTo("templateId", searchMap.get("templateId"));
            }

        }
        return example;
    }
}
